package com.five.wisdomClass.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.wisdomClass.config.PaginationUtil;
import com.five.wisdomClass.entity.*;
import com.five.wisdomClass.mapper.AdminMapper;
import com.five.wisdomClass.mapper.PublicCourseMapper;
import com.five.wisdomClass.mapper.TakeCourseMapper;
import com.five.wisdomClass.mapper.TeacherMapper;
import com.five.wisdomClass.service.IAdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl implements IAdminService {
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    TeacherMapper teacherMapper;


    // 发布公告
    @Override
    public boolean postAnnounce(Announce announce) {
        if (announce.getTitle()==null){
            return false;
        }
        if (announce.getContext() == null){
            return false;
        }
        return adminMapper.postAnnounce(announce) > 0;
    }

    // 查看公告
    @Override
    public List<Announce> getAnnounces() {
//        Page<Announce> pageRequest = new Page<>(currentPage,size);
//        IPage<Announce>  announceIpage = adminMapper.showAnnounce(pageRequest);
        return adminMapper.showAnnounce();
    }

    // 获取最新时间的一条公告
    @Override
    public Announce getLatestAnnounce() {
        return adminMapper.getLatestAnnounce();
    }


    // 查看请假列表
    @Override
    public IPage<Leave> getLeaveInfo(Integer currentPage, Integer size) {
        Page<Leave> pageRequest = new Page<>(currentPage,size);
        IPage<Leave>  leaveIPage = adminMapper.getLeaveStudents(pageRequest);
        return leaveIPage;
    }
    // 通过学生的请假申请
    @Override
    public boolean passStudentLeave(Leave leave) {
        if (leave == null){
            return false;
        }
        return adminMapper.passStudentLeave(leave) > 0;
    }

    // 驳回学生的请假申请
    @Override
    public boolean refuseStudentLeave(Leave leave) {
        if (leave == null){
            return false;
        }
        return adminMapper.refuseStudentLeave(leave) > 0;
    }

    // 获取课程调整申请
    @Override
    public IPage<Course> getInsertCourse(Integer currentPage,Integer size) {
        List<Course> courseList = new ArrayList<>();
        // 公共课申请
        List<Course> publicCourses = adminMapper.getUpdatePublicCourses();
        // 选修课申请
        List<Course> takeCourses = adminMapper.getUpdateTakeCourses();
        if (publicCourses != null){
            courseList.addAll(publicCourses);
        }
        if (takeCourses != null){
            courseList.addAll(takeCourses);
        }
        return PaginationUtil.paginate(courseList,currentPage,size);
    }

    // 审核教师添加课程申请
    @Override
    public boolean checkInsertCourse(Integer id, Integer state,String courseNumber) {
        // 判断课程是选修课还是公共课
        if (teacherMapper.checkPublic(courseNumber) > 0){           //是公共课
            return adminMapper.checkPublicCourse(id,state) > 0;
        }else if (teacherMapper.checkTake(courseNumber) > 0){    // 是选修课
            return adminMapper.checkTakeCourse(id,state) > 0;
        }else{
            return false;
        }
    }


}
